package com.ljkj.cordial.chat.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lanjiu.lib.imclient.dto.GroupVo;
import com.lanjiu.lib.utils.CollectionUtils;
import com.lanjiu.lib.utils.sp.SPKeys;
import com.lanjiu.lib.utils.sp.SharedPreferenceHelper;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.GroupMemberVercialAdapter;
import com.ljkj.cordial.chat.base.BaseActivity;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.notify.GroupMemberTable;
import com.ljkj.cordial.chat.ui.AppEventActivity;
import com.ljkj.cordial.chat.ui.views.GroupMemberAtelView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @群成员列表
 */
public class GroupMemberAtelActivity extends AppEventActivity<GroupMemberAtelView> {


    @BindView(R.id.listView)
    ListView listView;
    private GroupMemberVercialAdapter adapter;
    private String groupId;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_group_member_atel;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        groupId = getIntent().getStringExtra(Canstants.KEY_GROUP_ID);
        adapter = new GroupMemberVercialAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GroupMemberTable user = (GroupMemberTable) parent.getItemAtPosition(position);
                Intent intent = new Intent();
                intent.putExtra(Canstants.KEY_NICKNAME,TextUtils.isEmpty(user.getGroupMemberAlias())?user.getGroupMemberName():user.getGroupMemberAlias());
                intent.putExtra(Canstants.KEY_MEMBER_ID, user.getGroupMemberUserId());
                setResult(Canstants.RESULT_CODE_300, intent);
                finish();
            }
        });

    }

    @Override
    protected boolean afterOnStartRegisterEventBus() {
        if (!TextUtils.isEmpty(groupId)) {
            boolean isRequest = SharedPreferenceHelper.create(SPKeys.Name.CONFIG).getBoolean(SPKeys.Key.CONFIG_GROUP_MEMBER_REQUESTED + groupId, false);
            if (isRequest) {
                List<GroupMemberTable> list = GroupMemberTable.queryNoUser(groupId);
                if (!CollectionUtils.isEmpty(list))
                    adapter.setList(list);
            } else {
                obtainView().doGetGroupMemberList(groupId);
            }
        }
        return super.afterOnStartRegisterEventBus();
    }

    public static void jumpTo(Activity activity, String groupId) {
        Intent intent = new Intent(activity, GroupMemberAtelActivity.class);
        if (!TextUtils.isEmpty(groupId)) {
            intent.putExtra(Canstants.KEY_GROUP_ID, groupId);
        }
        activity.startActivityForResult(intent, Canstants.REQUEST_CODE_300);
        ChatApp.getInstance().inActivityAnim(activity);
    }

    @Override
    protected GroupMemberAtelView bindNettyMessageView() {
        return new GroupMemberAtelView((code, msg, data) -> {
            List<GroupMemberTable> list = GroupMemberTable.queryNoUser(groupId);
            adapter.setList(list);
        });
    }
}
